
# coding=utf-8
import json

import tushare as ts
from flask import request

from PythonWork import app
from PythonWork import db

@app.route('/get_movie_day', methods=['POST','GET'])
def get_movie_day():
    if not db.movie_day:
        df = ts.day_boxoffice()
        df_json = df.to_json(orient='records')
        df.movie_day.insert(json.loads(df_json))

    movie_day_collection = db.movie_day.find()
    data_json = []
    for c in movie_day_collection:
        ##如果直接返回movie_day_collection，提示id不能序列化
        m = {}
        m["SumBoxOffice"] = c["SumBoxOffice"]
        m["IRank"] = c["IRank"]
        m["AvgPrice"] = c["AvgPrice"]
        m["MovieDay"] = c["MovieDay"]
        m["BoxOffice"] = c["BoxOffice"]
        m["AvpPeoPle"] = c["AvpPeoPle"]
        m["MovieName"] = c["MovieName"]
        m["BoxOffice_Up"] = c["BoxOffice_Up"]
        data_json.append(m)
    return json.dumps(data_json,ensure_ascii=False,encoding="UTF-8")

@app.route('/get_movie_now', methods=['POST','GET'])
def get_movie_now():
    if not db.movie_now:
        df = ts.realtime_boxoffice()
        df_json = df.to_json(orient='records')
        df.movie_now.insert(json.loads(df_json))
    movie_now_collection = db.movie_now.find()
    data_json = []
    for c in movie_now_collection:
        ##如果直接返回movie_day_collection，提示id不能序列化
        m = {}
        m["BoxOffice"] = c["BoxOffice"]
        m["Irank"] = c["Irank"]
        m["MovieName"] = c["MovieName"]
        m["boxPer"] = c["boxPer"]
        m["movieDay"] = c["movieDay"]
        m["sumBoxOffice"] = c["sumBoxOffice"]
        m["time"] = c["time"]
        m["WomIndex"] = c["WomIndex"]
        data_json.append(m)
    return json.dumps(data_json, ensure_ascii=False, encoding="UTF-8")

@app.route('/validate_user', methods=['POST','GET'])
def validate_user():
    name = request.form["name"] or ""
    password = request.form["pass"] or ""
    validate = dict()
    validate["name_result"] = False
    validate["pass_result"] = False
    if name:
        if db.user.find({"name":name}).count() > 0:
            validate["name_result"] = True
    if password and name:
        if db.user.find({"name":name,"pass":password}).count() > 0:
            validate["pass_result"] = True
    return json.dumps(validate, ensure_ascii=False, encoding="UTF-8")

if __name__ == '__main__':
    app.run()

